﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

using TLL.Repository;
using TLL.Finance;
using TLL.Finance.Currencys;
using TLL.UTL;
using TLL.Debug;
using TLL.Log;

namespace TLL.Finance.Currencys
{
    public class CurrencyLogRepository : XMLRepository<Invoice>
    {
        private string debugURL = string.Empty;
        public CurrencyLogRepository(string url, string debugURL) : base(url, debugURL, true, true) { }
        public override List<Invoice> Get(int id = 0)
        {
            var res = new List<Invoice>();
            try
            {
                if (id == 0)
                    foreach (XmlNode node in this.Document.ChildNodes[1])
                        res.Add(new Invoice(
                            this.AttributeByType<int>(node, XMLCurrencyLogAttributes.ID),
                            Utilities.GetEnum<CurrencyType>(this.AttributeByType<string>(node, XMLCurrencyLogAttributes.Type)),
                            DateTime.Parse(this.AttributeByType<string>(node, XMLCurrencyLogAttributes.Date)),
                            LogTime.Parse(this.AttributeByType<string>(node, XMLCurrencyLogAttributes.Time)),
                            this.AttributeByType<double>(node, XMLCurrencyLogAttributes.Value)
                        ));
                    else
                        foreach (XmlNode node in this.Document.ChildNodes[1])
                            if (this.AttributeByType<int>(node, XMLCurrencyLogAttributes.ID) == id)
                            {
                                res.Add(new Invoice(
                                    this.AttributeByType<int>(node, XMLCurrencyLogAttributes.ID),
                                    Utilities.GetEnum<CurrencyType>(this.AttributeByType<string>(node, XMLCurrencyLogAttributes.Type)),
                                    DateTime.Parse(this.AttributeByType<string>(node, XMLCurrencyLogAttributes.Date)),
                                    LogTime.Parse(this.AttributeByType<string>(node, XMLCurrencyLogAttributes.Time)),
                                    this.AttributeByType<double>(node, XMLCurrencyLogAttributes.Value)
                                ));
                                break;
                            }
            }
            catch 
            {
                this.Error = new Error(ErrorType.Get, new Dictionary<string, string>() { 
                    { "ID", id.ToString() }
                });
                DebugLogRepository.Add(InformationType.Error, string.Format(@"CurrencyLogRepository, Get(id = {0})", id), this.debugURL); 
            }
            DebugLogRepository.Add(InformationType.Information, string.Format(@"CurrencyLogRepository, Get(int id = {0}). Success operation", id), this.debugURL);
            return res;
        }
        public override bool Create(Invoice item)
        {
            try
            {
                this.Document.ChildNodes[1].AppendChild(this.Create_Node(
                    "ItemLog",
                    this.Create_Attributes(new Dictionary<Enum, string>() {
                        { XMLCurrencyLogAttributes.ID, item.ID.ToString() },
                        { XMLCurrencyLogAttributes.Date, item.Date.ToShortDateString() },
                        { XMLCurrencyLogAttributes.Type, item.Type.ToString() },
                        { XMLCurrencyLogAttributes.Time, LogTime.Formated(item.Time) },
                        { XMLCurrencyLogAttributes.Value, item.Value.ToString() }
                    })
                ));
                DebugLogRepository.Add(InformationType.Information, string.Format(@"CurrencyLogRepository, Create(ItemLog item[ID = {0}]). Success operation", item.ID), this.debugURL);
            }
            catch 
            {
                this.Error = new Error(ErrorType.Create, new Dictionary<string, string>() { 
                        { "ID", item.ID.ToString() },
                        { "Date", item.Date.ToShortDateString() },
                        { "Type", item.Type.ToString() },
                        { "Time", LogTime.Formated(item.Time) },
                        { "Value", item.Value.ToString() }
                });
                this.Warning = new Warning(WarningType.Create);
                DebugLogRepository.Add(InformationType.Error, string.Format(@"CurrencyLogRepository, Create(ItemLog item[ID = {0}])
                                           Error title: {1}
                                           Error message: {2}", item.ID, Error.Title, Error.Message), this.debugURL);
                return false; 
            }
            return true;
        }

        public int GetLastId()
        {
            var items = Get().Select(x => x.ID).OrderBy(x => x).ToList();
            if (items.Count > 0)
                return items.LastOrDefault() + 1;
            else
                return 0;
        }
    }
}
